/*
 * Copyright (c) 2015 elion All Rights Reserved.
 */
package com.guoyu.project.common.velocity;

import java.io.IOException;
import java.io.Writer;

import org.apache.velocity.context.InternalContextAdapter;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.parser.node.Node;
import org.apache.velocity.runtime.parser.node.SimpleNode;

/**
 * overide块节点包装
 * 
 * @author: xblibo Date: 2015/5/18 Time: 13:49
 */
public class OverrideNodeWrapper extends SimpleNode {
    /**
     * 当前节点
     */
    Node current;
    /**
     * 父节点
     */
    OverrideNodeWrapper parentNode;

    public OverrideNodeWrapper(Node node) {
        super(1);
        this.current = node;
    }

    public boolean render(InternalContextAdapter context, Writer writer) throws IOException, MethodInvocationException,
            ParseErrorException, ResourceNotFoundException {
        OverrideNodeWrapper preNode = (OverrideNodeWrapper) context.get(VelocitySupports.OVERRIDE_CURRENT_NODE);
        try {
            context.put(VelocitySupports.OVERRIDE_CURRENT_NODE, this);
            return current.render(context, writer);
        } finally {
            context.put(VelocitySupports.OVERRIDE_CURRENT_NODE, preNode);
        }
    }

    public Node getCurrent() {
        return current;
    }

    public void setCurrent(Node current) {
        this.current = current;
    }

    public OverrideNodeWrapper getParentNode() {
        return parentNode;
    }

    public void setParentNode(OverrideNodeWrapper parentNode) {
        this.parentNode = parentNode;
    }
}
